package com.xkdx.caipiao.my;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.bank.AddBankInfo;
import com.xkdx.caipiao.presistence.bank.BindBankAction;
import com.xkdx.caipiao.presistence.bank.BindBankModule;
import com.xkdx.caipiao.presistence.bank.BindBankPresistener;
import com.xkdx.caipiao.presistence.bank.SupportBankListAction;
import com.xkdx.caipiao.presistence.bank.SupportBankListModule;
import com.xkdx.caipiao.presistence.bank.SupportBankListPresistener;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class AddBankActivity extends ThreadActivity implements OnClickListener {

	private ImageView back;
	private Spinner bank_spinner;

	private EditText bank_code;
	private TextView titleinfo;
	private Button commit;
	private SupportBankListAction action;
	private SupportBankListModule module;
	private SupportBankListPresistener presistener;
	private ArrayList<AddBankInfo> supportlist;

	private BindBankAction bindaction;
	private BindBankModule bindmodule;
	private BindBankPresistener Bindpresistener;
	SharePrefenceUtil sp;
	String uid, token, bankid, banktype, bankcode;
	ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_bank_caipiao);
		sp = new SharePrefenceUtil(AddBankActivity.this,
				SharePrefenceUtil.USERINFO);
		findView();

		startThread();
	}

	private void findView() {
		titleinfo = (TextView) findViewById(R.id.titleinfo);

		bank_spinner = (Spinner) findViewById(R.id.spinner_bank);
		bank_code = (EditText) findViewById(R.id.ed_bank_code);
		back = (ImageView) findViewById(R.id.back);
		commit = (Button) findViewById(R.id.btn_bank_commit);
		commit.setOnClickListener(this);
		back.setOnClickListener(this);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			supportlist = module.list;
			final String[] strid = new String[supportlist.size()];
			String[] strname = new String[supportlist.size()];
			for (int i = 0; i < supportlist.size(); i++) {
				strid[i] = supportlist.get(i).getBankId();
				strname[i] = supportlist.get(i).getBankName();
			}
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, strname);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			bank_spinner.setAdapter(adapter);
			// 添加下拉框选项选择**
			bank_spinner
					.setOnItemSelectedListener(new OnItemSelectedListener() {

						@Override
						public void onItemSelected(AdapterView<?> adapterView,
								View view, int position, long id) {
							banktype = strid[position];

						}

						@Override
						public void onNothingSelected(AdapterView<?> arg0) {

						}

					});

		} else {
			Toast.makeText(this, "支持的银行列表获取失败", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void startThread() {

		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		action = new SupportBankListAction();
		module = new SupportBankListModule();
		presistener = new SupportBankListPresistener(AddBankActivity.this);
		presistener.setActitons(action);
		presistener.setModule(module);
		presistener.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.btn_bank_commit:
			uid = usrSP.getId();
			token = usrSP.getLogintoken();
			bankcode = bank_code.getText().toString();
			if (isCorrect()) {
				StartBindBank();
			}
			break;
		}
	}

	private boolean isCorrect() {
		if (TextUtils.isEmpty(bankcode)) {
			Toast.makeText(getApplicationContext(), "银行卡号不能为空", 0).show();
			return false;
		} else {
			if (IsCorrectCodeRegular(bankcode)) {
				return true;
			} else {
				Toast.makeText(getApplicationContext(), "请输入正确格式的银行卡号", 0)
						.show();
				return false;
			}
		}

	}

	private boolean IsCorrectCodeRegular(String value) {

		String regular = "\\d{16,19}$";
		return value.matches(regular);

	}

	public void showOnBindPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			if ("0000".equals(bindmodule.code)) {
				toast("成功绑定银行");
				finish();
			} else {
				toast(bindmodule.message);
			}

		} else {
			toast("绑定银行失败");
		}
	}

	private void StartBindBank() {
		bindaction = new BindBankAction(uid, token, banktype, bankcode);
		bindmodule = new BindBankModule();
		Bindpresistener = new BindBankPresistener(AddBankActivity.this);
		Bindpresistener.setActitons(bindaction);
		Bindpresistener.setModule(bindmodule);
		Bindpresistener.execute();
	}

	@Override
	public void onBackClick(View view) {

	}

}
